package br.edu.oxente.buzz.service.dumb;

import br.edu.oxente.buzz.entity.Buzz;
import br.edu.oxente.buzz.entity.Comment;
import br.edu.oxente.buzz.entity.Post;
import br.edu.oxente.buzz.entity.User;
import br.edu.oxente.buzz.service.OBServices;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Nigini A. Oliveira - ensino@nigini.com.br
 */
public class OBDumbFacade implements OBServices {

	private Map<String,Buzz> buzzes;

	public OBDumbFacade() {
		this.buzzes = new HashMap<String, Buzz>();
	}
	
	public boolean addUser(String login) {
		if( !this.validateString(login) ) return false;
		if( this.buzzes.keySet().contains( login ) ){
			return false;
		} else {
			User newUser = new User( login, "" );
			Buzz newBuzz = new Buzz( newUser );
			this.buzzes.put( login, newBuzz );
			return true;
		}
	}

	public boolean addPost( String login, String text ) {
		if( !this.buzzes.keySet().contains( login ) ){
			return false;
		}
		if( !this.validateString(text) ) return false;

		Buzz b = this.buzzes.get( login );
		Post newPost = new Post( b.getOwner(), text );
		b.insertPost( newPost );
		for( User follower : b.getFollowers() ) {
			this.buzzes.get( follower.getLogin() ).insertPost( newPost );
		}
		return true;
	}

	public List<Post> listPosts(String login, int limit) {
		if( !this.buzzes.keySet().contains( login ) || limit < 0 ) {
			return null;
		}
		return this.buzzes.get(login).getPosts( limit );
	}

	public boolean followUser(String login, String userToFollow) {
		if( !this.buzzes.keySet().contains( login ) || !this.buzzes.keySet().contains( userToFollow ) ) {
			return false;
		}
		Buzz userBuzz = this.buzzes.get(login);
		Buzz toFollowBuzz = this.buzzes.get( userToFollow );
		boolean sucess = userBuzz.follow( toFollowBuzz.getOwner() );
		if( sucess ) {
			return toFollowBuzz.insertFollower( userBuzz.getOwner() );
		} else {
			return false;
		}
	}

	public boolean reshare(String login, Post toReshare) {
		Buzz user = this.buzzes.get( login );
		if( user != null ) {
			if( user.reShare( toReshare ) ) {
				for ( User follower : user.getFollowers() ) {
					Buzz followerBuzz = this.buzzes.get( follower.getLogin() );
					followerBuzz.insertPost(toReshare);
				}
				return true;
			}
		}
		return false;
	}

	public boolean comment( String login, Post post, String commentText ) {
		if(!this.validateString(commentText)) return false;
		
		Buzz user = this.buzzes.get( login );
		if( user != null ) {
			return user.insertComment(post, new Comment( user.getOwner() , commentText ) );
		}
		return false;
	}

	public boolean vote(String login, Post post) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public int countVotes(Post post) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	public boolean validateString( String toValidate ) {
		if( toValidate == null ) return false;
		toValidate = toValidate.trim();
		if( toValidate.equals( "" ) )return false;
		return true;
	}
}
